package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogResourcePathBo;
import com.ruoyix.weblog.domain.vo.BlogResourcePathVo;
import com.ruoyix.weblog.domain.BlogResourcePath;
import com.ruoyix.weblog.mapper.BlogResourcePathMapper;
import com.ruoyix.weblog.service.IBlogResourcePathService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资源路径Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class BlogResourcePathServiceImpl implements IBlogResourcePathService {

    private final BlogResourcePathMapper baseMapper;

    /**
     * 查询资源路径
     */
    @Override
    public BlogResourcePathVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询资源路径列表
     */
    @Override
    public TableDataInfo<BlogResourcePathVo> queryPageList(BlogResourcePathBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogResourcePath> lqw = buildQueryWrapper(bo);
        Page<BlogResourcePathVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询资源路径列表
     */
    @Override
    public List<BlogResourcePathVo> queryList(BlogResourcePathBo bo) {
        LambdaQueryWrapper<BlogResourcePath> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogResourcePath> buildQueryWrapper(BlogResourcePathBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogResourcePath> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BlogResourcePath::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getClassify()), BlogResourcePath::getClassify, bo.getClassify());
        lqw.eq(StringUtils.isNotBlank(bo.getCover()), BlogResourcePath::getCover, bo.getCover());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), BlogResourcePath::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getIntroduction()), BlogResourcePath::getIntroduction, bo.getIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), BlogResourcePath::getType, bo.getType());
        lqw.eq(bo.getStatus() != null, BlogResourcePath::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增资源路径
     */
    @Override
    public Boolean insertByBo(BlogResourcePathBo bo) {
        BlogResourcePath add = BeanUtil.toBean(bo, BlogResourcePath.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改资源路径
     */
    @Override
    public Boolean updateByBo(BlogResourcePathBo bo) {
        BlogResourcePath update = BeanUtil.toBean(bo, BlogResourcePath.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogResourcePath entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除资源路径
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
